<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    html,body{
      width: 100%;
    }
    h1{
      width: 100%;
      height: 30px;
      font-size: 14px;
      line-height: 30px;
      background-color: #f5f5f5;
      text-indent: 30px;
    }
    h1>a{
      text-decoration: none; 
      margin-top: 20px;
      margin-left: 5px;
      color: red;
      font-weight: normal;
    }
  </style>
</head>
<body>
  <h1>你好,请<a href="./03-login.html">登录</a></h1>
  <h1>欢迎,<span>xxx</span></h1>
  <a href="./08-list.html">列表页</a>
</body>
<script src="./axios.js"></script>
<script>
  let oH1= document.querySelectorAll('h1')
  let oSpan=document.querySelector('span')
  //默认让第一个显示,第二个隐藏
  oH1[1].style.display='none'

  //发送请求,如果登录了,就让span显示
  axios({
    url:'http://localhost:8888/users/info',
    method:'get',
    params:{
      id:localStorage.getItem('id')
    },
    //携带这个请求头,代表用户是登录过的
    headers:{
      'authorization':localStorage.getItem('token')
    }
  }).then(res=>{
    // console.log(res)
    //如果
    if(res.data.code !=1){
      return
    }
    oH1[0].style.display='none'
    oH1[1].style.display='block'
    oSpan.innerText=res.data.info.username
  })
</script>
</html>
